const app = getApp()
var pwd = "";
var userPassword="";
Page({
  
  //获取用户解锁密码
  getUserPassword: function () {
    var _this = this
    //获取本地存的userId
    var userId = app.globalData.userId
    //尝试获取云端用户信息
    wx.cloud.callFunction({
      name: 'get_password',
      data: {
        getSelf: true,
        userId:app.userId
      },
      success: res => {
        if (res.errMsg == "cloud.callFunction:ok")
          if (res.result) {
            //如果成功获取到
            //将获取到的用户资料写入app.js全局变量
            wx.redirectTo({
              url: '/pages/index/index'
            })
          } else {
            //未成功获取到用户信息
            //调用注册方法
            console.log("未设置密码")
            wx.redirectTo({
              url: '/pages/newpassword/index'
            })
          }
      },
      fail: err => {
        wx.showToast({
          title: '请检查网络您的状态',
          duration: 1000,
          icon: 'none'
        })
        console.error("get_password调用失败", err.errMsg)
      }
    })
  },

  data: {
    password: [],
    hint: '输入解锁密码',
    Length: 4,
    ispassword: false//是否显示星号
  },
  onLoad: function () {
    let _this = this
    //获取本地存的userId
    var userId = app.globalData.userId
    //尝试获取云端用户信息
    wx.cloud.callFunction({
      name: 'get_password',
      data: {
        getSelf: true,
        userId: app.globalData.userId
      },
      success: res => {
        if (res.errMsg == "cloud.callFunction:ok")
        if (res.result!=undefined) {
            //如果成功获取到
            //将获取到的用户密码写入app.js全局变量
            userPassword = "",
            userPassword = res.result
          } else {
            //未成功获取到用户信息
            //调用注册方法
            console.log("未设置密码")
            wx.redirectTo({
              url: '/pages/newpassword/index'
            })
          }
      },
      fail: err => {
        wx.showToast({
          title: '请检查网络您的状态',
          duration: 1000,
          icon: 'none'
        })
        console.error("get_password调用失败", err.errMsg)
      }
    })
  },

  keyword: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id
    let password = that.data.password
    password.push(id);
    if (password.length == this.data.Length) {
        pwd = ""
        for (let i = 0; i < password.length; ++i) {
          pwd += password[i];
        }
      if (pwd != "") {
        if (pwd == userPassword) {
          wx.switchTab({
            url: '/pages/index/index'
          })
        } else {
          wx.showToast({
            title: '密码错误',
            duration: 1000,
            icon: 'none'
          })
          that.cancel()
        }
      }
    }
    that.setData({
      password: password
    })

  },
  cancel: function (e) {
    let that = this;
    let password = that.data.password
    password = []
    that.setData({
      password: password
    })
  },
  delete: function (e) {
    let that = this;
    let password = that.data.password
    password.pop()
    that.setData({
      password: password
    })
  }
})
